$(document).ready(function() {
	getRewardApply();
	getProcessCondition();
});

//获取申请单基本信息
function getRewardApply(){
	$.ajax({ 
	 	url: ctp + "/rewardController/getRewardApplyByRecid.shtml",
	 	data: {"recId": recId,"auditId": auditId},
		type : 'POST',
		dataType: "json", 
		success : function(result) {
			if(result != null){
				var list = result.list;
				var data = list[0];
				$("#recTime").val(data.REC_TIME);
				$("#applyPerson").val(data.USER_NAME);
				$("#rewardType").val(data.REWARD_TYPE);
				$("#rewardLevel").val(data.REWARD_LEVEL);
				$("#rewardMoney").val(data.REWARD_MONEY);
				$("#workerName").val(data.WORKER_NAME);
				$("#happenTime").val(data.HAPPEN_TIME);
				$("#rewardDesc").val(data.REWARD_DESC);
				$("#rewardResult").val(data.REWARD_RESULT);
				
			}
		}
	});
}

//获取当前流程审核情况
function getProcessCondition(){
	$.ajax({ 
	 	url: ctp + "/rewardController/getProcessConditionByrecid.shtml",
	 	data: {"recId": recId,"isView":"2"},
		type : 'POST',
		dataType: "json", 
		success : function(result) {
			if(result != null){
				var list = result.list;
				for(var i = 0; i < list.length; i++){
					var data = list[i];
					if(data.PARENT_ID == "00"){
						addProcessContion(data);
					}
				}
				var parentId = '0';
				iterateProcess(list, parentId);
				
				//完成节点
				var html = '<div class="smart-state_item over">'
		             + '        <div class="smart-state_cir">'
		             + '    <i class="layui-icon">&#xe616;</i>'
		             + '</div>'
		             + '<div class="smart-state_content">'
		             + '    <span class="smart-state_name">完成</span>'
		             + '</div>'
		             + '</div>';
				$("#processContion").append(html);
			}
		}
	});
}

//循环遍历审批流程
function iterateProcess(list, parentId){
	for(var i = 0; i < list.length; i++){
		if(list[i].PARENT_ID == parentId){
			addProcessContion(list[i]);
			iterateProcess(list, list[i].AUDIT_ID);
		}
	}
}

function addProcessContion(json){
	var auditDesc = json.AUDIT_DESC;
	var recTime = json.REC_TIME;
	if(auditDesc == undefined){
		auditDesc = "";
	}
	if(recTime == undefined){
		recTime = "";
	}
	var className = '';
	if(json.AUDIT_STATE == "审核不通过"){
		className = 'state_reject';
	}else if(json.AUDIT_STATE == "审核通过"){
		className = 'state_success';
	}
	var html = '<div class="smart-state_item">'
			+'        <div class="smart-state_cir ' + className +'">'
			+'    <i class="layui-icon">&#xe617;</i>'
			+'</div>'
			+'<div class="smart-state_content">'
			+'    <span class="smart-state_name">' + json.USER_NAME + '</span>'
			+'    <span class="sscsp ' + className +'">【' + json.AUDIT_STATE + '】</span>'
			+'    <span class="smart-state_reason">' + auditDesc + '</span>'
			+'    <p class="smart-state_time">' + recTime + '</p>'
			+'</div>'
			+'</div>';
	$("#processContion").append(html);
	
}

function audit(value){
	$.ajax({ 
	 	url: ctp + "/rewardController/audit.shtml",
	 	data: {"auditId": auditId,"auditState": value,"submitId": recId,"auditDesc": $("#auditDesc").val()},
		type : 'POST',
		success : function(result) {
			if(result == ''){
				jalert9("审核成功！");
			}else{
				jalert(result);
			}
		}
	});
}